﻿using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using eCommerce.core.Helpers;
using eCommerce.db.SQLDataAccess;

namespace eCommerce.cms
{
    public partial class Customer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                ListCustomer();
        }

        private SqlConnection GetConnection()
        {
            var connectionString = ConfigurationManager.ConnectionStrings["MainConnectionString_Online"].ToString();
            var con = new SqlConnection(connectionString);
            con.Open();

            return con;
        }

        private void ListCustomer()
        {
            rptCustomer.DataSource = SqlHelper.ExecuteDataset(GetConnection(), "Customer_GetListAll");
            rptCustomer.DataBind();
        }

        protected void rptCustomer_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var ltTotalMoney = (Literal) e.Item.FindControl("ltTotalMoney");
                var customerId = Utils.ToInt32(((HiddenField)e.Item.FindControl("hfCustomerId")).Value);

                var totalMoney = SqlHelper.ExecuteDataset(GetConnection(), "Customer_GetTotalMoney_ByCustomerId", customerId).Tables[0].Rows[0][0];
                ltTotalMoney.Text = Utils.FormatPrice(Utils.ToDecimal(totalMoney));
            }
        }
    }
}